package com.scau.os;

import com.scau.os.course_manager.Clock.TimeManager;
import com.scau.os.course_manager.Scheduler;
import com.scau.os.extension.EventSystem;
import com.scau.os.extension.MyDelegate;
import com.scau.os.extension.Utility;
import com.scau.os.fileSystem.directory.File;
import com.scau.os.fileSystem.directory.FileManager;
import com.scau.os.fileSystem.memory.Disk;
import com.scau.os.fileSystem.other.FileManagerLauncher;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.security.SecureRandom;
import java.util.Arrays;


public class DesktopLauncher extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        FileManagerLauncher.INSTANCE.launch();
        new Scheduler();
        initFiles();
        FXMLLoader fxmlLoader = new FXMLLoader(DesktopLauncher.class.getResource("Desktop02.fxml"));
        Parent root=fxmlLoader.load();
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("桌面");
        primaryStage.getIcons().add(Utility.getFxImage("Image/Windows98Icon.png"));
        primaryStage.show();
        EventSystem.instance.register(new MyDelegate("demonstrationStart") {
            @Override
            public void execute(Object data) {
                demonstration();
            }
        }).UnRegisterWhenClose(DesktopLauncher.this);
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent windowEvent) {
                if (!TimeManager.getInstance().executor.isShutdown()) {
                    TimeManager.getInstance().executor.shutdown();
                }
                EventSystem.instance.UnRegisterAllBySource(DesktopLauncher.this);
                //windowEvent.consume();
                Platform.exit();
            }
        });
        root.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if(event.getCode()== KeyCode.K){
                    //test("execute /a/202.e");
                    //test("execute /a/b/303.e");
                    //TODO:进程随机选择执行函数
                    demonstration();
                }
            }
        });
    }

    public void initFiles() throws Exception {
        FileManager.INSTANCE.createDirectory("/a");
        FileManager.INSTANCE.createDirectory("/b");
        FileManager.INSTANCE.createDirectory("/a/a");
        FileManager.INSTANCE.createDirectory("/a/b");
        FileManager.INSTANCE.createDirectory("/a/c");
        FileManager.INSTANCE.createFile("/a/101.e");
        FileManager.INSTANCE.createFile("/a/202.e");
        FileManager.INSTANCE.createFile("/a/b/303.e");
        FileManager.INSTANCE.removeDirectory("/a/c");
        FileManager.INSTANCE.removeFile("/a/b/303.e");

        FileManager.INSTANCE.copyFile("/a/202.e", "/a/b");
        FileManager.INSTANCE.typeFile("/a/b/202.e");
        for(int i=10;i<=20;i++){
            String path="/a/b/"+i+".e";
            FileManager.INSTANCE.createFile(path);
            byte[] tmp;
            if(i<=13){
                tmp=new byte[] {(byte)0b00001010,(byte)0b01000001,(byte)0b01111111, (byte) 0b10001001,(byte)0b01000001,
                        (byte)0b00001001,(byte)0b01000001,(byte)0b01111111,(byte)0b11000000};
            }else if(i>13&&i<=16){
                tmp=new byte[] {(byte)0b00001010,(byte)0b01000001,(byte)0b01111111, (byte) 0b10011001,(byte)0b01000001,
                        (byte)0b00000101,(byte)0b01000001,(byte)0b01111111,(byte)0b11000000};
            }else {
                tmp=new byte[] {(byte)0b00001010,(byte)0b01000001,(byte)0b01111111, (byte) 0b10101001,(byte)0b01000001,
                        (byte)0b00000011,(byte)0b01000001,(byte)0b01111111,(byte)0b11000000};
            }

            FileManager.INSTANCE.writeFile(path,tmp);
        }
        byte[] tmp={(byte)0b00001010,(byte)0b01000001,(byte)0b01111111,(byte)0b01000001,
                (byte)0b00000001,(byte)0b01000001,(byte)0b01111111,(byte)0b11000000};
        FileManager.INSTANCE.writeFile("/a/202.e",tmp);
        tmp=new byte[] {(byte)0b00001010,(byte)0b01000001,(byte)0b01111111, (byte) 0b10001001,(byte)0b01000001,
                (byte)0b00000001,(byte)0b01000001,(byte)0b01111111,(byte)0b11000000};
        FileManager.INSTANCE.writeFile("/a/101.e",tmp);
        FileManager.INSTANCE.typeFile("/a/202.e");
    }
    private void demonstration(){
        SecureRandom random=new SecureRandom();

        Boolean[] v=new Boolean[10];
        for (int i=0;i<10;i++){
            v[i]=false;
        }
        for(int i=10;i<20;i++){
            String path="/a/b/"+i+".e";
            while(true){
                int r=random.nextInt(10);
                if(r<10&&v[r]==false){
                    v[r]=true;
                    int randomTime=random.nextInt(10);
                    TimeManager.getInstance().addDelayTime(()->{
                        try {
                            FileManager.INSTANCE.executeFile(path);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    },randomTime);
                    break;
                }
            }

        }
    }
}
